package cz.muni.fi.pa165.library.web_layer.adminPages.Book;

import cz.muni.fi.pa165.library.api.Available;
import cz.muni.fi.pa165.library.api.BookService;
import cz.muni.fi.pa165.library.api.BookTO;
import cz.muni.fi.pa165.library.api.Genre;
import cz.muni.fi.pa165.library.api.State;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.Template;
import java.util.Arrays;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class EditBook extends Template {
	private static final long serialVersionUID = 1L;

    private BookTO book;    
    private static final List<State> STATES = Arrays.asList(State.values());
    private static final List<Genre> GENRES = Arrays.asList(Genre.values());
    
    public EditBook(final PageParameters parameters) {
	super(parameters);
        BookService bookService = (BookService) ApplicationContextProvider.getApplicationContext().getBean("bookService");
        StringValue bookId = parameters.get("bookId");
        if (bookId.isEmpty()) {
           book = new BookTO();
           add(new Label("titleMessage", new Model("Create book")));
        } else {
            book = bookService.findBookById(bookId.toLong());
            add(new Label("titleMessage", new Model("Edit book")));
        }
        
        Form<?> form = new Form("form") {
            @Override
            protected void onSubmit() {
                BookService bookService = (BookService) ApplicationContextProvider.getApplicationContext().getBean("bookService");
                if (book.getId() == null) {
                    book.setAvailability(Available.AVAILABLE);
                    bookService.createBook(book);
                } else {
                    bookService.updateBook(book);
                }
                setResponsePage(ShowAllBook.class);
            }
        };
        add(form);

        DropDownChoice choiceState=new DropDownChoice("status", new PropertyModel(book, "status"), STATES);
        form.add(choiceState.setRequired(true));
        DropDownChoice choiceGenre=new DropDownChoice("genre", new PropertyModel(book, "genre"), GENRES);
        form.add(choiceGenre.setRequired(true));
        
        form.add(new TextField("title", new PropertyModel<String>(book, "title")).setRequired(true));
        form.add( new TextField("author", new PropertyModel<String>(book, "author")).setRequired(true));
        form.add(new TextField("publicationYear", new PropertyModel<String>(book, "publicationYear")).setRequired(true));
        form.add(new TextField( "publicationPlace", new PropertyModel<String>(book, "publicationPlace")).setRequired(true));
        form.add(new TextField("publisher", new PropertyModel<String>(book, "publisher")).setRequired(true));
        form.add(new TextField("ISBN", new PropertyModel<String>(book, "ISBN")).setRequired(true));

        BookmarkablePageLink books = new BookmarkablePageLink<>(
                "list", ShowAllBook.class);
        add(books);
    }
}
